﻿(function (me){	var scriptName = "エクスプレッションキラー";	var mes = "";	//----------------------------------------
	//プロパティからレイヤを得る。	var getLayerFromProp = function(prop)
	{
		var ret = null;
		var p = prop;		var pp = prop;
		while ( p != null){
			if (			(p instanceof AVLayer)			||(p instanceof ShapeLayer)			||(p instanceof TextLayer)			||(p instanceof LightLayer)			||(p instanceof CameraLayer)			) {				ret = p;				break;			}			p = p.parentProperty;		}
		return ret;
	}
	//----------------------------------------	//アクティブなコンポを得る。なかったらnull
	var getActiveComp = function(){		var ac = app.project.activeItem;		if ( !(ac instanceof CompItem) ) {			return null;		}else{			return ac;		}			}	//----------------------------------------	//重複するキーフレームを削除
	var killkeys = function(prop)	{		var b = false;		switch (prop.propertyValueType)		{			case PropertyValueType.ThreeD_SPATIAL:			case PropertyValueType.ThreeD:			case PropertyValueType.COLOR:			case PropertyValueType.TwoD_SPATIAL:			case PropertyValueType.TwoD:			case PropertyValueType.OneD:				b = true;				break;			default:				b = false;				break;		}		if (b==false) return;		var k = prop.numKeys;		if ( k>=3){			for ( var i= k-1; i>=2; i--)			{				var p = prop.keyValue(i).toString();				var p0 = prop.keyValue(i-1).toString();				var p1 = prop.keyValue(i+1).toString();				if (p == p0) {					if (p == p1) {						prop.removeKey(i);					}				}			}			if (prop.numKeys == 2){				var s0 = prop.keyValue(1).toString();				var s1 = prop.keyValue(2).toString();				if (s0==s1) {					prop.removeKey(2);					prop.removeKey(1);				}			}		}	}	//----------------------------------------	//エクスプレッションをキーフレームに変換。
	var killExp = function(cmp, layer, prop)	{		if ((prop.canSetExpression===true)&&(prop.expressionEnabled === true )) {			try{				var frm = Math.floor(cmp.duration * cmp.frameRate);				for ( var i=0 ;i<= frm; i++){					var tm = i  / cmp.frameRate;					prop.setValueAtTime(tm,prop.valueAtTime(tm,false));				}				prop.expressionEnabled = false;			}catch(e){				alert(e.toString());			}			killkeys(prop);			try{				mes += prop.name +"(" + cmp.name +"/" + layer.name + ")\n";			}catch(e){				alert(e.toString());			}		}	}	//----------------------------------------	//プロパティを再帰的に調べていく
	var propsExec = function(cmp, layer, prop)	{		if (prop == null) return;		if (prop instanceof Property){			killExp(cmp, layer, prop);		}else if (prop instanceof PropertyGroup){			if (prop.numProperties>0){				for ( var i=1; i<=prop.numProperties;i++){					propsExec(cmp, layer, prop.property(i));				}			}		}	}	//----------------------------------------	//プロパティ検索の出発点
	var propsExecMain = function(cmp,layer)	{		propsExec(cmp, layer, layer.property("ADBE Effect Parade"));		propsExec(cmp, layer, layer.property("ADBE Transform Group"));		propsExec(cmp, layer, layer.property("ADBE Root Vectors Group"));	}	//----------------------------------------	var kilExpresiionFromProp = function()	{		var ac = getActiveComp();		if (ac==null) {			alert("コンポをアクティブにしてください");			return;		}		if (ac.selectedProperties.length<=0) {			alert("プロパティを選択してください");			return;		}				mes = "";		app.beginUndoGroup(scriptName+":選択プロパティ");		var props = ac.selectedProperties;		for ( var i=0; i<props.length; i++){			var layer = getLayerFromProp(props[i]);			killExp(ac, layer, props[i]);		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}	//----------------------------------------	var kilExpresiionFromLayer = function()	{		var ac = getActiveComp();		if (ac==null) {			alert("コンポをアクティブにしてください");			return;		}		if (ac.selectedLayers.length<=0) {			alert("レイヤーィを選択してください");			return;		}		mes = "";		app.beginUndoGroup(scriptName+":選択レイヤー");		for (var i=0; i<ac.selectedLayers.length; i++){			propsExecMain(ac, ac.selectedLayers[i]);		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}	//----------------------------------------	var kilExpresiionFromComp = function()	{		var sel = [];		if (app.project.selection.length>0) {			for ( var i=0; i<app.project.selection.length; i++){				if (app.project.selection[i] instanceof CompItem) {					sel.push(app.project.selection[i]);				}			}		}		if ( sel.length<=0) {			alert("コンポを選んでください。");			return;		}		mes = "";		app.beginUndoGroup(scriptName+":選択コンポ");		for ( var c=0; c<sel.length; c++){			var cmp = sel[c];			if (cmp.numLayers>0) {				for (var i=1; i<=cmp.numLayers; i++){					propsExecMain(cmp, cmp.layers[i]);				}			}		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}	//----------------------------------------	var kilExpresiionFromProj = function()	{		var sel = [];		if (app.project.numItems>0) {			for ( var i=1; i<=app.project.numItems; i++){				if (app.project.items[i] instanceof CompItem) {					sel.push(app.project.items[i]);				}			}		}		if ( sel.length<=0) {			alert("コンポがないです。");			return;		}		mes = "";		app.beginUndoGroup(scriptName+":プロジェクト");		for ( var c=0; c<sel.length; c++){			var cmp = sel[c];			if (cmp.numLayers>0) {				for (var i=1; i<=cmp.numLayers; i++){					//alert(cmp.name +"/"+cmp.layers[i].name);					propsExecMain(cmp, cmp.layers[i]);				}			}		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}	//********************************************************************	//----------------------------------------	var expOn = function(cmp, layer, prop)	{		if ((prop.canSetExpression===true)&&(prop.expressionEnabled === false )) {			try{				prop.expressionEnabled = true;				mes += prop.name +"(" + cmp.name +"/" + layer.name + ")\n";			}catch(e){				alert(e.toString());			}		}	}	//----------------------------------------	var propsOn = function(cmp, layer, prop)	{		if (prop == null) return;		if (prop instanceof Property){			expOn(cmp, layer, prop);		}else if (prop instanceof PropertyGroup){			if (prop.numProperties>0){				for ( var i=1; i<=prop.numProperties;i++){					propsOn(cmp, layer, prop.property(i));				}			}		}	}	//----------------------------------------	var propsOnMain = function(cmp,layer)	{		propsOn(cmp, layer, layer.property("ADBE Effect Parade"));		propsOn(cmp, layer, layer.property("ADBE Transform Group"));		propsOn(cmp, layer, layer.property("ADBE Root Vectors Group"));	}	//----------------------------------------	var expOnFromProp = function()	{		var ac = getActiveComp();		if (ac==null) {			alert("コンポをアクティブにしてください");			return;		}		if (ac.selectedProperties.length<=0) {			alert("プロパティを選択してください");			return;		}				mes = "";		app.beginUndoGroup(scriptName+":EXP_ON 選択プロパティ");		var props = ac.selectedProperties;		for ( var i=0; i<props.length; i++){			var layer = getLayerFromProp(props[i]);			expOn(ac, layer, props[i]);		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}		//----------------------------------------	var expOnFromLayer = function()	{		var ac = getActiveComp();		if (ac==null) {			alert("コンポをアクティブにしてください");			return;		}		if (ac.selectedLayers.length<=0) {			alert("レイヤーィを選択してください");			return;		}		mes = "";		app.beginUndoGroup(scriptName+": ON 選択レイヤー");		for (var i=0; i<ac.selectedLayers.length; i++){			propsOnMain(ac, ac.selectedLayers[i]);		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}	//----------------------------------------	var expOnFromComp = function()	{		var sel = [];		if (app.project.selection.length>0) {			for ( var i=0; i<app.project.selection.length; i++){				if (app.project.selection[i] instanceof CompItem) {					sel.push(app.project.selection[i]);				}			}		}		if ( sel.length<=0) {			alert("コンポを選んでください。");			return;		}		mes = "";		app.beginUndoGroup(scriptName+": ON 選択コンポ");		for ( var c=0; c<sel.length; c++){			var cmp = sel[c];			if (cmp.numLayers>0) {				for (var i=1; i<=cmp.numLayers; i++){					propsOnMain(cmp, cmp.layers[i]);				}			}		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}
	//----------------------------------------	var expOnFromProj = function()	{		var sel = [];		if (app.project.numItems>0) {			for ( var i=1; i<=app.project.numItems; i++){				if (app.project.items[i] instanceof CompItem) {					sel.push(app.project.items[i]);				}			}		}		if ( sel.length<=0) {			alert("コンポがないです。");			return;		}		mes = "";		app.beginUndoGroup(scriptName+":ON プロジェクト");		for ( var c=0; c<sel.length; c++){			var cmp = sel[c];			if (cmp.numLayers>0) {				for (var i=1; i<=cmp.numLayers; i++){					//alert(cmp.name +"/"+cmp.layers[i].name);					propsOnMain(cmp, cmp.layers[i]);				}			}		}		app.endUndoGroup();		if (mes =="") {			mes = "変換なし!";		}		alert(mes);	}	//********************************************************************	var winObj = ( me instanceof Panel) ? me : new Window("palette", scriptName, [ 0,  0,  100,  210] ,{resizeable:true, maximizeButton:true, minimizeButton:true});
	
	var x = 5;	var y = 5;	var h = 30;	var w = 90;	var yh = 35;	var stKiller = winObj.add("statictext", [  x,   y,   x +  w,   y +  h], "Exp. キラー");
	y += yh;	var lstKiller = winObj.add("dropdownlist", [  x,   y,   x +  w,   y +  h], ["プロパティ対象", "レイヤ対象", "コンポ対象","プロジェクト対象"]);
	y += yh;	lstKiller.items[0].selected = true;	var btnKillerExec = winObj.add("button", [  x,   y,   x +  w,   y +  h], "キラー実行");
	y += yh;		var stON = winObj.add("statictext", [  x,   y,   x +  w,   y +  h], "Exp. ON");
	y += yh;	var lstON = winObj.add("dropdownlist", [  x,   y,   x +  w,   y +  h], ["プロパティ対象", "レイヤ対象", "コンポ対象","プロジェクト対象"]);
	y += yh;	lstON.items[0].selected = true;	var btnONExec = winObj.add("button", [  x,   y,   x +  w,   y +  h], "ON実行");
	y += yh;		//********************************************************************	var execKiller = function()	{		switch(lstKiller.selection.index)		{			case 0: kilExpresiionFromProp(); break;			case 1: kilExpresiionFromLayer(); break;			case 2: kilExpresiionFromComp(); break;			case 3: kilExpresiionFromProj(); break;			default:				alert("execKiller/" + lstKiller.selection.index );				break;		}	}	//********************************************************************	var execON = function()	{		switch(lstON.selection.index)		{			case 0: expOnFromProp(); break;			case 1: expOnFromLayer(); break;			case 2: expOnFromComp(); break;			case 3: expOnFromProj(); break;			default:				alert("execON / " + lstON.selection.index);				break;		}	}	//********************************************************************	var resize = function ()
	{
		var btnSet = function(btn) {
			var ob = winObj.bounds;
			var w = ob[2] - ob[0];
			var b = btn.bounds;			if (b != null) {
				b[2] = w - 5;
				btn.bounds = b;			}
		}
		btnSet(stKiller);
		btnSet(lstKiller);
		btnSet(btnKillerExec);
		btnSet(stON);
		btnSet(lstON);
		btnSet(btnONExec);
	}
	resize();
	winObj.addEventListener("resize",resize);
	winObj.onResize = resize;
	//********************************************************************	btnKillerExec.onClick = execKiller;	btnONExec.onClick = execON;	//----------------------------------------
	if ( ( me instanceof Panel) == false){
		winObj.center();
		winObj.show();
	}

})(this);